package com.sinoglobal.ningxia.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;

/**
 * 
 * @author zhourihu
 * @createdate 2013-12-24 上午11:44:46
 * @Description: TODO(用一句话描述该类做什么) 自定义对话框 context 必须实现 MyDialogListener 接口；
 */
public class MyDialog extends Dialog implements
		android.view.View.OnClickListener {
	public static final int POSITIVE_BUTTON = R.id.btn_1;
	public static final int NEGATIVE_BUTTON = R.id.btn_2;
	public Button btnPositive, btnNegative;
	public TextView tvTitle, tvMessage;
	MyDialogListener myDialogListener;

	public MyDialog setMyDialogListener(MyDialogListener myDialogListener) {
		this.myDialogListener = myDialogListener;
		return this;
	}

	/**
	 * 默认隐藏确定按钮和取消按钮
	 */
	public MyDialog(Context context) {
		// 设置主题去黑边
		super(context, R.style.dialog);
		if (context instanceof MyDialogListener) {
			myDialogListener = (MyDialogListener) context;
		}
		View view = LayoutInflater.from(context).inflate(R.layout.dialog, null);
		btnPositive = (Button) view.findViewById(R.id.btn_1);
		btnNegative = (Button) view.findViewById(R.id.btn_2);
		tvTitle = (TextView) view.findViewById(R.id.title_text);
		tvMessage = (TextView) view.findViewById(R.id.message_text);
		btnPositive.setOnClickListener(this);
		btnNegative.setOnClickListener(this);
		btnNegative.setVisibility(View.GONE);
		btnPositive.setVisibility(View.GONE);
		setContentView(view);
	}

	public MyDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		//Auto-generated constructor stub
	}

	public MyDialog(Context context, int theme) {
		super(context, theme);
	}

	public MyDialog setTitle(String text) {
		tvTitle.setText(text);
		tvTitle.setVisibility(View.VISIBLE);
		return this;
	}

	public MyDialog setMessage(String text) {
		tvMessage.setText(text);
		tvMessage.setVisibility(View.VISIBLE);
		return this;
	}

	public MyDialog setPositiveButton(String text) {
		btnPositive.setText(text);
		btnPositive.setVisibility(View.VISIBLE);
		return this;
	}

	public MyDialog setNegativeButton(String text) {
		btnNegative.setText(text);
		btnNegative.setVisibility(View.VISIBLE);
		return this;
	}

	@Override
	public void setContentView(int layoutResID) {
		//Auto-generated method stub
		super.setContentView(layoutResID);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_1:// 确定按钮
			myDialogListener.doPositive();
			break;
		case R.id.btn_2:// 取消按钮
			myDialogListener.doNegative();
			break;

		default:
			break;
		}
		dismiss();
	}

	public static interface MyDialogListener {
		void doPositive();

		void doNegative();
	}

}
